import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))

const enabledData = [{ name:'启用', value: '1' }, { name:'禁用', value:'0' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板名称',			name: 'report_name' },
			{ label: '模板编号',			name: 'report_num' },
			{ label: '模板英文名称',		name: 'report_en' },
			{ label: '状态',			name: 'enabled',	type: 'select', data: enabledData },
			{ label: '创建时间',			names: ['created_at_start_date','created_at_end_date'], type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '名称',			name: 'report_name', 	half:true,required:true },
			{ label: '英文名称',			name: 'report_en', 		half:true },
			{ label: '路径',			name: 'route', 			half:true, type:'select', idStr:'code' },
			{ label: '类型',			name: 'type', 			half:true, type:'select', idStr:'code' },
			{ label: '数据格式',			name: 'json_data', 		full:true, type:'textarea',p:'格式必须为JSON格式' },
			{ label: '说明',			name: 'remark', 				   type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getModalSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	//获取弹窗下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 41000, 61950 ]})
			// 路径
			$fn.setRow(formData, 'route', dic['41000'])
			// 类型
			$fn.setRow(formData, 'type', dic['61950'])
			
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'kd-report-from/index')
	
	// 表格
	cols = [
		{ title: '模板名称', 		dataIndex: 'report_name', 		width:150 },
		{ title: '模板编号', 		dataIndex: 'report_num', 		width:150 },
		{ title: '模板英文名称', 		dataIndex: 'report_en', 	width:150 },
		{ title: '状态', 			dataIndex: 'enabled', 			...$fn.table.status, render:text=>$fn.getState(enabledData,text) },
		{ title: '路径', 			dataIndex: 'route_name', 		width:100 },
		{ title: '模板版本', 		dataIndex: 'version_num', 		width:100 },
		{ title: '说明', 			dataIndex: 'remark', 			width:150 },
		{ title: '类型', 			dataIndex: 'type_name', 		width:100 },
		{ title: '更新时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		{ title: 'token', 			dataIndex: 'token', 			width:280 },
		TableControls.controls.call(this,{ width: 200 })
	]
	// 表格操作按钮
	table_controls = (text, rows) => {
		const { uuid } = rows
		return [
			TableControls.edit.call(this, 'kd-report-from/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),															// 删除
			TableControls.pushTo.call(this, '查看版本', `check-version?rows=${$fn.encode(JSON.stringify(rows))}` ),
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect}),			// 添加
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'kd-report-from/del', { uuid, isKeys: !uuid, onComplete:()=>$fn.dataRemove('kd-report-from/select') })
	render(){
		return (
			<>
				<Page title='报告单模板' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'kd-report-from/add', editApi:'kd-report-from/edit', onComplete:()=>$fn.dataRemove('kd-report-from/select') }) }
			</>
		)
	}
}

export default Index